//
//  ADButton.m
//  UIbutton封装
//
//  Created by adiljan on 16/4/15.
//  Copyright © 2016年 adiljan. All rights reserved.
//

#import "ADButton.h"

#define KTITLERADIO 0.2
@implementation ADButton

// 修改属性
+ (id)buttonWithType:(UIButtonType)buttonType frame:(CGRect)frame title:(NSString *)title image:(UIImage *)image handler:(tapHandler)handler{
    
    ADButton *button = [super buttonWithType:buttonType];
    button.frame = frame;
    [button setTitle:title forState:UIControlStateNormal];
    button.titleLabel.textAlignment = NSTextAlignmentCenter;
    button.titleLabel.font = [UIFont systemFontOfSize:16];
    [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    button.imageView.contentMode = UIViewContentModeCenter;
    [button setImage:image forState:UIControlStateNormal];
    button.handler = handler;
    [button addTarget:button action:@selector(btnTapped:) forControlEvents:UIControlEventTouchUpInside];
    
    return button;
}

// 点击事件
-(void)btnTapped:(UIButton *)sender{
    if (self.handler) {
        self.handler(sender);
    }

}

#pragma 调整内部 imageView 的frame
- (CGRect)imageRectForContentRect:(CGRect)contentRect{
    CGFloat imageX = 0;
    CGFloat imageY = 0;
    CGFloat imageWidth = contentRect.size.width;
    CGFloat imageHeight = contentRect.size.height * (1-KTITLERADIO);
    return CGRectMake(imageX, imageY, imageWidth, imageHeight);
}

#pragma 调整内部 UIlabel 的frame
- (CGRect)titleRectForContentRect:(CGRect)contentRect{
    CGFloat titleX = 0;
    CGFloat titleHeight = contentRect.size.height * KTITLERADIO;
    CGFloat titleY = contentRect.size.height - titleHeight-3;
    CGFloat titleWidth = contentRect.size.width;
    return CGRectMake(titleX, titleY, titleWidth, titleHeight);
}
@end
